//
//  YYLNavigationController.swift
//  XinlangWeibo
//
//  Created by yangyinglei on 2017/3/26.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

import UIKit

class YYLNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        //如果设置了leftBarButtonItem  边缘返回的功能就没有了  需要自己去手势代理来实现
        self.interactivePopGestureRecognizer?.delegate = self 
    }

    //监听push操作
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.viewControllers.count > 0 {
            //如果当前的控制的的个数是1 说名是根视图控制器
            if self.viewControllers.count == 1 {
                let title = self.viewControllers.first?.title
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title!,imageName:"navigationbar_back_withtext", target: self, action: #selector(leftBtnAction))
                //设置标题
                viewController.navigationItem.title = "第\(self.viewControllers.count + 1)级控制器"
            } else {
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回",imageName:"navigationbar_back_withtext", target: self, action: #selector(leftBtnAction))
                //设置标题
                viewController.navigationItem.title = "第\(self.viewControllers.count + 1)级控制器"
            }
               
        }

//        print("当前控制器的个数\(self.viewControllers.count)")
        super.pushViewController(viewController, animated: animated)
//        print("当前控制器的个数\(self.viewControllers.count)")
    }
    //
    @objc private func leftBtnAction() {
        //返回
        self.popViewController(animated: true)
    }
    //MARK:边缘手势他代理
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        //如果是跟视图控制器不需要添加手势
        if self.viewControllers.count == 1 {
        return false
        }
        return true
    }
    
}
